package com.sskj.eng.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sskj.core.exception.MException;
import com.sskj.dingtalk.utils.HttpHelper;
import com.sskj.eng.domain.EngQueryParam;
import com.sskj.eng.domain.entity.DingJianDuEng;
import com.sskj.eng.domain.entity.ListEngResponse;
import com.sskj.eng.domain.entity.ListResponse;
import com.sskj.eng.repository.DUserRepository;
import com.sskj.eng.vo.RestParamVO;
import com.sskj.eng.vo.RestQueryVO;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping(value = "/jd/eng", method = { RequestMethod.POST, RequestMethod.GET })
public class EngServiceImpl {

	protected final Log logger = LogFactory.getLog(EngServiceImpl.class);

	@Value("${sskj.conf.restUrl}")
	private String restUrl;

	@Autowired
	DUserRepository dUserRepository;

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/page", method = RequestMethod.GET)
	@ApiOperation(value = "查询工程", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "keyword", value = "关键字", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "jdfzr", value = "监督组成员", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "pageNumber", value = "页号", paramType = "form", dataType = "int"),
			@ApiImplicitParam(name = "pageSize", value = "页的大小", paramType = "form", dataType = "int") })
	public List<DingJianDuEng> queryPage(@ApiIgnore EngQueryParam engQueryParam, @ApiIgnore Pageable pageable)
			throws MException {

		List<DingJianDuEng> result = new ArrayList<DingJianDuEng>();

		RestQueryVO data = new RestQueryVO();

		data.setPage(pageable.getPageNumber());
		data.setPageSize(pageable.getPageSize());
		data.setNameQueryName("dd.jiandueng");

		RestParamVO param = new RestParamVO();
		param.setPn("engName");
		param.setValue(engQueryParam.getKeyword());
		data.getParams().add(param);

		RestParamVO param2 = new RestParamVO();
		param2.setPn("jdfzr");
		param2.setValue(engQueryParam.getJdfzr());
		data.getParams().add(param2);

		try {
			JSONObject obj = HttpHelper.httpPost(restUrl + "engcontrol/queryJDEngListPage", data);
			if (obj != null) {
				result = JSON.parseArray(obj.getString("resultls"), DingJianDuEng.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new MException("request_timeout");
		}

		return result;

	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/count", method = RequestMethod.GET)
	@ApiOperation(value = "查询工程", httpMethod = "GET")
	public Integer countPage(@ApiIgnore EngQueryParam engQueryParam, @ApiIgnore Pageable pageable) throws MException {

		Integer result = 0;

		RestQueryVO data = new RestQueryVO();

		data.setPage(pageable.getPageNumber());
		data.setPageSize(pageable.getPageSize());
		data.setNameQueryName("dd.jiandueng");

		RestParamVO param = new RestParamVO();
		param.setPn("engName");
		param.setValue(engQueryParam.getKeyword());
		data.getParams().add(param);

		RestParamVO param2 = new RestParamVO();
		param2.setPn("jdfzr");
		param2.setValue(engQueryParam.getJdfzr());
		data.getParams().add(param2);

		try {
			JSONObject obj = HttpHelper.httpPost(restUrl + "engcontrol/queryJDEngListPage", data);
			if (obj != null) {
				result = Integer.parseInt(obj.getString("totalRecordCount"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new MException("request_timeout");
		}

		return result;

	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/dd/page", method = RequestMethod.GET)
	@ApiOperation(value = "查询工程", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "keyword", value = "关键字", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "jdfzr", value = "监督组成员", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "pageNumber", value = "页号", paramType = "form", dataType = "int"),
			@ApiImplicitParam(name = "pageSize", value = "页的大小", paramType = "form", dataType = "int") })
	public ListResponse queryDdPage(@ApiIgnore EngQueryParam engQueryParam, @ApiIgnore Pageable pageable)
			throws MException {
		ListResponse rep = new ListResponse();

		List<DingJianDuEng> result = new ArrayList<DingJianDuEng>();

		RestQueryVO data = new RestQueryVO();

		data.setPage(pageable.getPageNumber() - 1);
		data.setPageSize(pageable.getPageSize());
		data.setNameQueryName("dd.jiandueng");

		RestParamVO param = new RestParamVO();
		param.setPn("engName");
		param.setValue(engQueryParam.getKeyword());
		data.getParams().add(param);

		RestParamVO param2 = new RestParamVO();
		param2.setPn("jdfzr");
		param2.setValue(engQueryParam.getJdfzr());
		data.getParams().add(param2);

		try {
			JSONObject obj = HttpHelper.httpPost(restUrl + "engcontrol/queryJDEngListPage", data);
			if (obj != null) {
				result = JSON.parseArray(obj.getString("resultls"), DingJianDuEng.class);
				rep.setSuccess(true);
				rep.setContent(new ListEngResponse(pageable.getPageNumber(), result));
			}
		} catch (Exception e) {
			e.printStackTrace();
			rep.setSuccess(false);
			throw new MException("request_timeout");
		}

		return rep;

	}

}
